/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.Clinic;
import br.com.foxline.clinicplus.entity.Customer;
import br.com.foxline.clinicplus.enums.Gender;
import br.com.foxline.clinicplus.enums.TypeIncome;
import br.com.foxline.clinicplus.enums.TypeProcedure;
import br.com.foxline.clinicplus.service.ClinicService;
import br.com.foxline.clinicplus.service.CustomerService;
import br.com.foxline.clinicplus.service.DiaryService;
import br.com.foxline.clinicplus.service.OperatorService;
import br.com.foxline.clinicplus.service.SystemUserService;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.chart.CartesianChartModel;
//import org.primefaces.component.chart.donut.DonutChart;
import org.primefaces.model.chart.DonutChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerIndex implements Serializable {

    @EJB
    SystemUserService systemUserService;
    @EJB
    CustomerService customerService;
    @EJB
    DiaryService diaryService;
    @EJB
    OperatorService operatorService;
    @EJB
    ClinicService clinicService;
    private DonutChartModel gender;
    private PieChartModel pieGender;
    private CartesianChartModel linearProcedureDate;
    private Long max;
    private List<Customer> customers;
    private Clinic clinic;

    @PostConstruct
    public void init() {
        max = 0L;
        gender = graficoGender();
        pieGender = graficoPieGender();
        if (isAdmin()) {
            linearProcedureDate = initLinearModel();
        }
        clinic = clinicService.findAll().get(0);
        
        
        FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();
    }

    public boolean aniversarioHoje(Customer c) {
        if (c != null && c.getDateBirth() != null) {
            Calendar gc = GregorianCalendar.getInstance();
            gc.setTime(c.getDateBirth());
            Calendar gc1 = GregorianCalendar.getInstance();
            if (gc.get(Calendar.DAY_OF_MONTH) == gc1.get(Calendar.DAY_OF_MONTH)) {
                return true;
            }
        }
        return false;
    }

    public void proxAniversariante() {
        Calendar gc = GregorianCalendar.getInstance();
        gc.add(GregorianCalendar.DAY_OF_MONTH, -1);
        gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
        gc.set(GregorianCalendar.MINUTE, 0);
        gc.set(GregorianCalendar.SECOND, 0);
        gc.set(GregorianCalendar.MILLISECOND, 0);

        Calendar gc1 = GregorianCalendar.getInstance();

        gc1.set(GregorianCalendar.DAY_OF_MONTH, gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
        gc1.set(GregorianCalendar.HOUR_OF_DAY, 23);
        gc1.set(GregorianCalendar.MINUTE, 59);
        gc1.set(GregorianCalendar.SECOND, 59);
        gc1.set(GregorianCalendar.MILLISECOND, 59);

        customers = customerService.aniversariantes(gc.getTime(), gc1.getTime());

    }

    public boolean isAdmin() {
        return systemUserService.verifyAdmin();
    }

    public boolean isMaster() {
        return systemUserService.verifyMaster();
    }

    public DonutChartModel graficoGender() {

        DonutChartModel model = new DonutChartModel();

        Map<String, Number> circle1 = new LinkedHashMap<String, Number>();
        circle1.put("Homens", customerService.countGenderCustomer(Gender.MASCULINO));
        circle1.put("Mulheres", customerService.countGenderCustomer(Gender.FEMININO));
        model.addCircle(circle1);

        return model;
    }

    public PieChartModel graficoPieGender() {

        PieChartModel model = new PieChartModel();

        model.set("Homens", customerService.countGenderCustomer(Gender.MASCULINO));
        model.set("Mulheres", customerService.countGenderCustomer(Gender.FEMININO));

        return model;
    }

    private CartesianChartModel initLinearModel() {
        CartesianChartModel model = new CartesianChartModel();

        LineChartSeries series1 = new LineChartSeries("Consultas");
        LineChartSeries series2 = new LineChartSeries("Exames");

        Calendar gc = GregorianCalendar.getInstance();
        gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
        gc.set(GregorianCalendar.MINUTE, 0);
        gc.set(GregorianCalendar.SECOND, 0);
        gc.set(GregorianCalendar.MILLISECOND, 0);
        gc.set(GregorianCalendar.DAY_OF_MONTH, 1);


        int mesAtual = gc.get(Calendar.MONTH);

        gc.set(GregorianCalendar.MONTH, 0);

        Calendar gc1 = GregorianCalendar.getInstance();
        gc1.set(GregorianCalendar.HOUR_OF_DAY, 23);
        gc1.set(GregorianCalendar.MINUTE, 59);
        gc1.set(GregorianCalendar.SECOND, 59);
        gc1.set(GregorianCalendar.MILLISECOND, 59);
        gc1.set(GregorianCalendar.MONTH, 0);


        gc1.set(GregorianCalendar.DAY_OF_MONTH, gc.getActualMaximum(Calendar.DAY_OF_MONTH));

        Locale brasil = new Locale("pt", "BR");
        SimpleDateFormat sdf = new SimpleDateFormat("MMMMM", brasil);

        for (int i = 0; i <= mesAtual; i++) {

            Long exame = diaryService.getValueIncomeGeneric(null, null, null, gc.getTime(), gc1.getTime(), null,
                    null, TypeIncome.C, null, null, null, null, null,
                    TypeProcedure.EXAME, null);

            //= diaryService.countDiaryForDate(gc.getTime(), gc1.getTime(),);

            Long consulta = diaryService.getValueIncomeGeneric(null, null, null, gc.getTime(), gc1.getTime(), null,
                    null, TypeIncome.C, null, null, null, null, null,
                    TypeProcedure.CONSULTA, null);

            if (max < exame) {
                max = exame;
            }
            if (max < consulta) {
                max = consulta;
            }

            series2.set(sdf.format(gc.getTime()), exame);
            series1.set(sdf.format(gc.getTime()), consulta);

            gc.add(GregorianCalendar.MONTH, 1);
            gc1.add(GregorianCalendar.MONTH, 1);
            gc1.set(GregorianCalendar.DAY_OF_MONTH, gc.getActualMaximum(Calendar.DAY_OF_MONTH));

        }
        max += 50;
        model.addSeries(series1);
        model.addSeries(series2);
        return model;
    }

    public DonutChartModel getGender() {
        return gender;
    }

    public void setGender(DonutChartModel gender) {
        this.gender = gender;
    }

    public PieChartModel getPieGender() {
        return pieGender;
    }

    public void setPieGender(PieChartModel pieGender) {
        this.pieGender = pieGender;
    }

    public CartesianChartModel getLinearProcedureDate() {
        return linearProcedureDate;
    }

    public void setLinearProcedureDate(CartesianChartModel linearProcedureDate) {
        this.linearProcedureDate = linearProcedureDate;
    }

    public Long countCustomer() {
        return customerService.countCustomer();
    }

    public Long countProcedureExame() {
        return diaryService.countDiary(TypeProcedure.EXAME);
    }

    public Long countProcedureConsulta() {
        return diaryService.countDiary(TypeProcedure.CONSULTA);
    }

    public Long countOperator() {
        return operatorService.countOperator();
    }

    public Long getMax() {
        return max;
    }

    public void setMax(Long max) {
        this.max = max;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    public Clinic getClinic() {
        return clinic;
    }

    public void setClinic(Clinic clinic) {
        this.clinic = clinic;
    }

}
